package com.jewelry.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jewelry.common.util.Constants;
import com.jewelry.service.IConfigAttrService;
import com.jewelry.service.ITemplateService;
import com.jewelry.vo.ConfigAttrVO;
import com.jewelry.vo.StatisticsVO;
import com.jewelry.vo.TemplateVO;

public class TemplateAction extends HttpAction {

	private static final long serialVersionUID = 2798504077408515523L;

	private static Logger logger = LoggerFactory
			.getLogger(TemplateAction.class);

	private ITemplateService templateService;

	private IConfigAttrService configAttrService;
	
	public IConfigAttrService getConfigAttrService() {
		return configAttrService;
	}

	public void setConfigAttrService(IConfigAttrService configAttrService) {
		this.configAttrService = configAttrService;
	}
	
	//模板款型
	private List<ConfigAttrVO> typeAttrList;
	
	//石料类型
	private List<ConfigAttrVO> stoneAttrList;
	
	private String stoneAttrSelectString;
	
	//石料形状
	private List<ConfigAttrVO> shapeAttrList;
	
	private String shapeAttrSelectString;
	
	public String getStoneAttrSelectString() {
		return stoneAttrSelectString;
	}

	public void setStoneAttrSelectString(String stoneAttrSelectString) {
		this.stoneAttrSelectString = stoneAttrSelectString;
	}

	public String getShapeAttrSelectString() {
		return shapeAttrSelectString;
	}

	public void setShapeAttrSelectString(String shapeAttrSelectString) {
		this.shapeAttrSelectString = shapeAttrSelectString;
	}

	public List<ConfigAttrVO> getShapeAttrList() {
		return shapeAttrList;
	}

	public void setShapeAttrList(List<ConfigAttrVO> shapeAttrList) {
		this.shapeAttrList = shapeAttrList;
	}

	public List<ConfigAttrVO> getStoneAttrList() {
		return stoneAttrList;
	}

	public void setStoneAttrList(List<ConfigAttrVO> stoneAttrList) {
		this.stoneAttrList = stoneAttrList;
	}

	public List<ConfigAttrVO> getTypeAttrList() {
		return typeAttrList;
	}

	public void setTypeAttrList(List<ConfigAttrVO> typeAttrList) {
		this.typeAttrList = typeAttrList;
	}

	private TemplateVO template;
	
	private List<TemplateVO> templateList;
	
	public List<TemplateVO> getTemplateList() {
		return templateList;
	}

	public void setTemplateList(List<TemplateVO> templateList) {
		this.templateList = templateList;
	}

	public TemplateVO getTemplate() {
		return template;
	}

	public void setTemplate(TemplateVO template) {
		this.template = template;
	}

	private List<StatisticsVO> statisticsList;

	public List<StatisticsVO> getStatisticsList() {
		return statisticsList;
	}

	public void setStatisticsList(List<StatisticsVO> statisticsList) {
		this.statisticsList = statisticsList;
	}

	public ITemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(ITemplateService templateService) {
		this.templateService = templateService;
	}

	// ===============================================================

	public String templateAdminMain() {
		try {
			statisticsList = templateService.getGroupByType();
		} catch (Exception e) {
			logger.error("getGroupByType Error!", e);
		}
		return SUCCESS;
	}
	
	public String queryTemplateList(){
		typeAttrList = configAttrService.getConfigAttrListByConfigGroup(Constants.TEMPLATE_TYPE_COONFIG);
		if(template==null){
			template = new TemplateVO();
		}
		try{
			templateList = templateService.getTemplateListByPage(template);
		}catch(Exception e){
			logger.error("getTemplateListByPage error!",e);
		}
		return SUCCESS;
	}
	
	public String addTemplatePage(){
		try{
			typeAttrList = configAttrService.getConfigAttrListByConfigGroup(Constants.TEMPLATE_TYPE_COONFIG);
			stoneAttrList = configAttrService.getConfigAttrListByConfigGroup(Constants.STONE_TYPE_COONFIG);
			shapeAttrList = configAttrService.getConfigAttrListByConfigGroup(Constants.SHAPE_TYPE_COONFIG);
			stoneAttrSelectString = configAttrService.getSelectTagString("attr.buildingStones", "buildingStones_id", "w50", stoneAttrList);
			shapeAttrSelectString = configAttrService.getSelectTagString("attr.shape", "shape_id", "w50", shapeAttrList);
		}catch(Exception e){
			logger.error("addTemplatePage error!",e);
		}
		template = null;
		return SUCCESS;
	}

	public String addingTemplate(){
		Map<String,String[]> templateAttrMap = httpRequest.getParameterMap();
		try{
			templateService.addTemplateAndAttr(template,templateAttrMap);
			message="模板保存成功!";
		}catch(Exception e){
			logger.error("addTemplateAndAttr error!",e);
			message="模板保存失败,请检查提交的数据!";
		}
		return SUCCESS;
	}
	
	public String getQueryTemplate() throws IOException{
		
		templateList = templateService.getTemplateList(template);
		StringBuilder sb = new StringBuilder();
		if(templateList.size()==0){
			sb.append("error");
		}else{
			for(TemplateVO vo:templateList){
				sb.append(vo.getTemplateid()).append(":");
				sb.append(vo.getTemplateName()).append(":");
				sb.append(vo.getImage1()).append(";");
			}
		}
		httpResponse.setContentType("text/plain;charset=UTF-8");
		PrintWriter writer = httpResponse.getWriter();
		writer.write(sb.toString());
		writer.flush();
		writer.close();
		
		return SUCCESS;
	}
}
